<?php

namespace App\Plugin\HostServer\Admin;


use App\Plugin\Website\Models\PnHostSvNav;
use Illuminate\Http\Request;


class NavController extends BaseController
{
    public $pageName='网站导航';
    public function setModel()
    {
        return $this->model=new PnHostSvNav();
    }
    public function createEditData($show = '')
    {
        $data = $this->model::category(request()->input('group_type', 'nav'));

        return ['cate' => $data];
    }
    public function checkRule($id = '')
    {
        if (!$id) {
            return [
                'name' =>[
                    "required"

                ],


            ];
        }
        return [
            'name' =>[
                "required"
            ],



        ];
    }
    public function indexData()
    {
        $item = [];
        $item['all_create_url'] = action($this->route['controller_name'] . '@allCreate', \request()->all());
        $item['all_post_url'] = action($this->route['controller_name'] . '@allCreatePost');
        $item['navs'] = [
            [
                'id' => 'nav',
                'name' => '主导航'
            ],
            [
                'id' => 'footer',
                'name' => '底部导航'
            ]
        ];
        return $item;
    }
    public function apiJsonItemExtend($item)
    {

        $item['pid'] = $item['parent_id'];
        return $item;
    }

    /**
     * 批量添加设置数据
     * @param Request $request
     * @return array|void
     */
    public function allCreateSetData(Request $request){
        $name_arr=explode("\n",$request->input('name'));
        $ename_arr=explode("\n",$request->input('url'));
        $all=$request->all();
        $arr=[];
        if(empty($name_arr))
        {
            return [];
        }

        foreach ($name_arr as $k=>$v)
        {

            unset($all['_token']);
            $all['name']=$v;
            $all['url']=$ename_arr[$k]??'';

            $arr[]=$all;
        }
        return $arr;
    }
    public  function allAfterEvent()
    {
        PnHostSvNav::updateCache();
    }

}
